package com.zdzx.base.common.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.zdzx.model.service.base.result.UserInfo;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;

public class UserInfoUtil {
	
	
	/**
	 * 序列化
	 * 
	 * @param object
	 * @return
	 */
	public static byte[] serialize(Object object) {
		ObjectOutputStream oos = null;
		ByteArrayOutputStream baos = null;
		try {
			// 序列化
			baos = new ByteArrayOutputStream();
			oos = new ObjectOutputStream(baos);
			oos.writeObject(object);
			byte[] bytes = baos.toByteArray();
			return bytes;
		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 反序列化
	 * 
	 * @param bytes
	 * @return
	 */
	public static Object unserialize(byte[] bytes) {
		ByteArrayInputStream bais = null;
		try {
			// 反序列化
			bais = new ByteArrayInputStream(bytes);
			ObjectInputStream ois = new ObjectInputStream(bais);
			return ois.readObject();
		} catch (Exception e) {

		}
		return null;
	}

/*	public static Object getLoginInfo(HttpServletRequest request) {
		String bytes = request.getHeader("userInfo");
		if(StringUtils.isBlank(bytes)) {
			return null;
		}*/
		/*
		 * if(StringUtils.isBlank(bytes)) return null;
		 */
		/*byte[] decode = Base64.getDecoder().decode(bytes);
		Object obj = SerializeUtil.unserialize(decode);
		return obj;
	}*/

	@SuppressWarnings("unchecked")
	public static <T> T  getLoginInfo(Class<T> clazz,RedisUtil redisUtil,HttpServletRequest request) {
		String token = request.getHeader("Authorization");
		if(StringUtils.isEmpty(token))
			return null;
		Map<String, Object> parseJWT1 = JwtUtil
				.parseJWT(JwtUtil.generateKey(JwtUtil.JWT_ALG, JwtUtil.JWT_RULE), token);
		if((boolean) parseJWT1.get("result")) {
		Jws<Claims> parseJWT = (Jws<Claims>) parseJWT1.get("code");
		String userId = parseJWT.getBody().getId();
		String subject = parseJWT.getBody().getSubject();
		String key=TokenInfo.getKey(userId,subject);
		Object userInfo=redisUtil.get(key);
		return JSON.parseObject(JSON.toJSONString(userInfo), clazz);
		}
		return null;
	}
	
	
	@SuppressWarnings("unchecked")
	public static String  getKey(RedisUtil redisUtil,HttpServletRequest request) {
		String token = request.getHeader("Authorization");
		if(StringUtils.isEmpty(token))
			return null;
		System.out.println(token+"-------");
		Map<String, Object> parseJWT1 = JwtUtil
				.parseJWT(JwtUtil.generateKey(JwtUtil.JWT_ALG, JwtUtil.JWT_RULE), token);
		if((boolean) parseJWT1.get("result")) {
		Jws<Claims> parseJWT = (Jws<Claims>) parseJWT1.get("code");
		String userId = parseJWT.getBody().getId();
		String subject = parseJWT.getBody().getSubject();
		String key=TokenInfo.getKey(userId,subject);
		return key;
		}
		return null;
				
	}

	public static void updateLogInfo(UserInfo userInfo, RedisUtil redisUtil, HttpServletRequest request) {
		String key = getKey(redisUtil, request);
		redisUtil.set(key,userInfo);
	}
}
